<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-calendar"></i> 菜单管理 </el-breadcrumb-item>
                <el-breadcrumb-item>菜单详情</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form ref="form" :model="form" label-width="80px" :rules="rules">
                    <el-form-item label="菜单名称" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>

                    <el-form-item label="菜单级别" prop="level">
                        <el-select v-model="form.level" placeholder="级别" disabled>
                            <el-option label="一级菜单" value="01"> </el-option>
                            <el-option label="二级菜单" value="02"> </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="父级菜单" prop="parentId" v-if="form.level == '02'">
                        <el-select v-model="form.parentId">
                            <el-option value="" label="请选择父级菜单"></el-option>
                            <el-option v-for="(item, index) in firstMenu" :key="index" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="图标" prop="icon" v-if="form.level == '01'">
                        <el-input v-model="form.icon" placeholder="请输入图标的代码"></el-input>
                    </el-form-item>

                    <el-form-item label="路径" prop="url" v-if="form.level == '02'">
                        <el-input v-model="form.url" placeholder="请输入路径"></el-input>
                    </el-form-item>

                    <el-form-item label="状态">
                        <el-switch v-model="form.status" :active-value="'01'" :inactive-value="'00'"></el-switch>
                    </el-form-item>

                    <el-form-item label="资源描述" prop="description">
                        <el-input type="textarea" rows="5" v-model="form.description"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="goback" icon="el-icon-arrow-left">返回</el-button>
                        <el-button type="primary" @click="onSubmit('form')" icon="el-icon-edit">修改菜单</el-button>
                        <el-button type="warning" @click="reset('form')" icon="el-icon-refresh-right">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'baseform',
    inject: ['reload'],
    data() {
        return {
            id: this.$route.query.id,
            // permissions: [],
            types: [],
            firstMenu: [],
            menu: {},
            form: {
                id: '',
                name: '',
                level: '',
                parentId: '',
                icon: '',
                url: '',
                status: '',
                description: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入菜单名称', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                parentId: [{ required: true, message: '请选择父菜单类型', trigger: 'blur' }],
                url: [{ required: true, message: '请输入路径', trigger: 'blur' }]
            }
        };
    },
    created: function () {
        this.init();
    },
    methods: {
        async init() {
            var a = await this.$http.get('/menu/' + this.id);
            if (a.code == 200) {
                this.menu = a.data;
                this.initForm(this.menu);
                if (this.form.level == '02') {
                    this.getFirstMenu();
                    console.log(this.menu.parentId);
                }
            } else {
                this.$message.error(a.message);
            }
        },

        async getFirstMenu() {
            let c = {
                level: '01',
                status: '01'
            };
            var d = await this.$http.post('/menuMap', c);
            if (d.code == 200) {
                this.firstMenu = d.data;
            }
        },
        initForm(menu) {
            this.form.id = this.id;
            this.form.name = menu.name;
            this.form.parentId = menu.parentId;
            this.form.icon = menu.icon;
            this.form.url = menu.url;
            this.form.level = menu.level;
            this.form.status = menu.status;
            this.form.description = menu.description;
        },

        goback() {
            this.$router.go(-1);
        },

        onSubmit(formName) {
            this.form.id = this.id;
            this.$refs[formName].validate(async (valid) => {
                if (this.form.level == '01') {
                    this.form.url = '';
                    this.parentId = '00';
                }
                if (this.form.level == '02') {
                    this.icon = '';
                }
                if (valid) {
                    var d = await this.$http.put('/menu', this.form);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.init;
                    } else {
                        this.$message.error(d.message);
                    }
                } else {
                    this.$message.warning('请把内容填写完整!');
                }
            });
        },

        reset(formName) {
            this.$confirm('此操作将重置所有信息, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.initForm(this.menu);
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消重置'
                    });
                });
        }
    }
};
</script>